
 /**
 * HibernateUserDao.java.java.
 * User: BrianChen Date: 2012-6-5 下午04:04:06
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.security.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.collapsar.core.orm.hibernate.BaseHibernateDao;
import com.collapsar.core.orm.page.Page;
import com.collapsar.core.security.dao.IUserDao;
import com.collapsar.core.security.model.User;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-5 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-5 下午04:04:06
 * @version 1.0
 * @see
 * 
 */
public class HibernateUserDao extends BaseHibernateDao implements IUserDao{

	@Override
	public void storeUser(User user) {
		this.store(user);
	}

	@Override
	public void deleteAllUsers(Long[] userIds) {
		this.deleteAll(User.class, userIds); 
	}

	@Override
	public void deleteUser(User user) {
		this.delete(user);
	}

	@Override
	public User loadUser(Long userId) {
		return (User)this.load(User.class, userId);
	}
	
	//????????????????
	@Override
	public User getUser(Long userId) {
		return (User)this.get(User.class, userId);
	}
	
//	@Override
//	public User loadUserWithGroupsAndRoles(Long userId){
//		User user = this.loadUser(userId);
//		this.initalize(user.getGroups());
//		this.initalize(user.getRoles());
//		return user;
//	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User getUserByAccount(final String userAccount) {
		User user = (User)this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.getNamedQuery("GetUserByAccount")
							.setParameter("userAccount", userAccount)
							.uniqueResult();
					}}
				);
//		this.initalize(user.getGroups());
//		this.initalize(user.getRoles());
		return user;
	}
	
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Override
//	public User getUserByName(final String userName) {
//		User user = (User)this.getHibernateTemplate().execute(
//				new HibernateCallback(){
//					@Override
//					public Object doInHibernate(Session session)
//							throws HibernateException, SQLException {
//						return session.getNamedQuery("GetUserByName")
//							.setParameter("nameName", userName)
//							.uniqueResult();
//					}}
//				);
//		this.initalize(user.getGroups());
//		this.initalize(user.getRoles());
//		return user;
//	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<User> findAllUsers(int offset, int pageSize) {
		String hql = "from User u";
		return findAll(offset, pageSize, hql);
	}
	

}

